/**
 * jims
 */
package com.jims.master.modules.reg.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.service.ClinicMasterService;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 病人就诊信息Controller
 * @author 赵宁
 * @version 2017-02-28
 */
@Controller
@RequestMapping(value = "${adminPath}/oper/clinicMaster")
public class ClinicMasterController extends BaseController {
	@Autowired
	private ClinicMasterService clinicMasterService;

	@ModelAttribute
	public ClinicMaster get(@RequestParam(required=false) String id) {
		ClinicMaster entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = clinicMasterService.get(id);
		}
		if (entity == null){
			entity = new ClinicMaster();
		}
		return entity;
	}

    /**
     * 挂号查询
     * @param request
     * @param response
     * @param clinicMaster
     * @return String
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"searchRegister", ""})
     public String searchRegister(Model model,ClinicMaster clinicMaster,HttpServletRequest request,HttpServletResponse response){
        String orgId = UserUtils.getOrgId();
        clinicMaster.setOrgId(orgId);
        if(clinicMaster.getClinicType()==null || clinicMaster.getClinicType().equals("")){
            clinicMaster.setClinicType("0");
        }
        Page<ClinicMaster> page = clinicMasterService.searchClinicRegInfo(new Page<ClinicMaster>(request, response), clinicMaster);
        model.addAttribute("page", page);
        model.addAttribute("clinicMaster",clinicMaster);
        return "modules/reg/searchRegister";
     }
	/**
	 * 加载退号管理页面
	 * @param clinicMaster
	 * @param request
	 * @param response
	 * @param model
	 * @return String
     * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"searchClinicRegInfo", ""})
	public String list(ClinicMaster clinicMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
		String orgId = UserUtils.getOrgId();
		clinicMaster.setOrgId(orgId);
		Page<ClinicMaster> page = clinicMasterService.searchClinicRegInfo(new Page<ClinicMaster>(request, response), clinicMaster);
		model.addAttribute("page", page);
		return "modules/reg/backClinicReg";
	}
    /**
     * 加载退号管理页面(信息科)
     * @param clinicMaster
     * @param request
     * @param response
     * @param model
     * @return String
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"infoBackRegist", ""})
    public String infoBackRegist(ClinicMaster clinicMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
        String orgId = UserUtils.getOrgId();
        clinicMaster.setOrgId(orgId);
        Page<ClinicMaster> page = clinicMasterService.searchClinicRegInfo(new Page<ClinicMaster>(request, response), clinicMaster);
        model.addAttribute("page", page);
        return "modules/reg/infoBackClinicReg";
    }

	/**
	 * 退号
	 * @param clinicMaster
	 * @return StringData
     * @author 赵宁
	 */
    @RequiresPermissions("user")
    @RequestMapping(value = "backClinicForReg")
    @ResponseBody
    public StringData save(ClinicMaster clinicMaster) {
        String userId=UserUtils.getUser().getId();
        StringData data=new StringData();
        int i=clinicMasterService.backReg(clinicMaster,userId);
        if(i>0){
            data.setCode("success");
            data.setData("退号成功");
        }else{
            data.setCode("error");
            data.setData("退号失败");
        }
        return data;
    }

    /**
     * 转诊操作
     * @param id 就诊记录 Id
     * @param deptId 科室id
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "updateReferral")
    @ResponseBody
    public StringData updateReferral(String id,String deptId) {
        StringData data=new StringData();
        ClinicMaster clinicMaster=clinicMasterService.get(id);
        Integer i=0;
        if(clinicMaster!=null){
            clinicMaster.setReferralNum(1);
            clinicMaster.setDeptId(deptId);
            i=clinicMasterService.save(clinicMaster);
        }
        if(i>0){
            data.setCode("success");
            data.setData("转诊成功");
        }else{
            data.setCode("error");
            data.setData("转诊失败");
        }
        return data;
    }

    /**
	 * 修改待诊为已诊
	 * @param clinicMaster
	 * @return StringData
     * @author fd
	 */
    @RequiresPermissions("user")
    @RequestMapping(value = "updateClinicTypeOrMyPat")
    @ResponseBody
    public StringData updateClinicTypeOrMyPat(ClinicMaster clinicMaster) {
        StringData data=new StringData();
        String doctorId=clinicMaster.getDoctorId();
        if(doctorId == null){
            User user=UserUtils.getUser();
            clinicMaster.setDoctorId(user.getId());
        }
        int i=clinicMasterService.updateClinicType(clinicMaster);
        if(i>0){
            data.setCode("success");
            data.setData("修改成功");
        }else{
            data.setCode("error");
            data.setData("修改失败");
        }
        return data;
    }

    /**
     * 挂号打印
     * @param clinicMaster
     * @param id
     * @param request
     * @param response
     * @param model
     * @return String
     * @author chentao
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "clinicPrint")
    public String print(ClinicMaster clinicMaster,String id,HttpServletRequest request, HttpServletResponse response, Model model) {
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        clinicMaster = clinicMasterService.clinicMasterPrint(clinicMaster);
        model.addAttribute("printTime",time);
        model.addAttribute("clinicMaster",clinicMaster);
        return "modules/reg/clinicPrint";
    }
//    /**
//     * @Author chenxy
//     * @param patMasterIndex
//     * @param request
//     * @param response
//     * @param model
//     * @return
//     */
//    @RequiresPermissions("user")
//    @RequestMapping(value = {"patsIndex", ""})
//    public String patsIndex(PatMasterIndex patMasterIndex, HttpServletRequest request, HttpServletResponse response, Model model) {
//           patMasterIndex.updateOrgId();
//           boolean isHaveDept= patMasterIndex.getDeptId()==null || "".equals(patMasterIndex.getDeptId());
//           if(isHaveDept&&patMasterIndex.getIsDept()!=null){
//               List<String> deptIdStr=new ArrayList<String>();
//               String  deptIdsQ="";
//               // '1','2' 查询权限科室
//               String userId=UserUtils.getUser().getId();
//               List<String> groups=this.orgGroupVsUserService.findGroupByUserId(userId);
//               for(String groupId:groups){
//                   //根据分组Id查询科室Id
//                   List<String> deptIds=this.orgGroupVsDeptService.findDeptIdByGroupId(groupId);
//                   deptIdStr.addAll(deptIds);
//               }
//               for(String s:deptIdStr){
//                   deptIdsQ=deptIdsQ+"'"+s+"'"+",";
//               }
//               deptIdsQ=deptIdsQ.substring(0,deptIdsQ.length()-1);
//               patMasterIndex.setDeptId(deptIdsQ);
//           }
//            List<PatMasterIndex> myPats=this.clinicMasterService.getPatsByDoctor(patMasterIndex);
//            model.addAttribute("myPats", myPats);
//            model.addAttribute("myPatsCount", myPats.size());
//            return "modules/doctor/clinicIndex";
//    }

    /**
     * 打开科室对话框
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "openDept")
    public String selectUserToRole(Model model) {
         return "modules/doctor/selectDept";
    }

    /**
     * 门诊医生站首次加载
     * 将医生的权限科室Ids 传到页面
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"patsIndex", ""})
    public String clinicMain(HttpServletRequest request, HttpServletResponse response, Model model) {
         return "modules/doctor/clinicIndex";
    }

    /**
     * 根据页面多条件查询患者列表
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findPatientByConditions", ""})
    @ResponseBody
    public Page<ClinicMaster> findPatientByConditions(ClinicMaster clinicMaster,HttpServletRequest request, HttpServletResponse response){
        Page<ClinicMaster>page=new Page<ClinicMaster>(request, response);
        User user=UserUtils.getUser();
        if("1".equals(clinicMaster.getPowerDeptIds())){
            OrgDept orgDept=new OrgDept();
            orgDept.setId(clinicMaster.getDeptId());
            orgDept.setDeptName(clinicMaster.getDeptName());
            user.setOrgDept(orgDept);
            UserUtils.updateCache(user);
        }else{
            clinicMaster.setDoctorId(user.getId());
        }
        Page<ClinicMaster> page1=clinicMasterService.findPatientByConditionsPage(page,clinicMaster);
        return page1;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"findById", ""})
    @ResponseBody
    public ClinicMaster findById(String id,HttpServletRequest request, HttpServletResponse response) {
        ClinicMaster entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = clinicMasterService.get(id);
        }
        if (entity == null) {
            entity = new ClinicMaster();
        }
        return entity;
    }
}